package com.bitspilani.infonet;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity{
	private static TextView accMeter;
	private static SensorManager mSensorManager;
	private static Sensor mAccSensor;
	private static Sensor mGravitySensor;
	private static AccelerometerHandler accHandler = new AccelerometerHandler();
	private static RelativeLayout activityLayout;
	private static Context activityContext;

	
	public MainActivity(){
		
	}
	

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// activityContext = getApplicationContext();
		activityLayout =(RelativeLayout)findViewById(R.id.activityLayout);

		System.out.println("On create fired");

		accMeter = (TextView) findViewById(R.id.accMeter);
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mGravitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

		mSensorManager.registerListener(accHandler, mAccSensor, SensorManager.SENSOR_DELAY_NORMAL);
		mSensorManager.registerListener(accHandler, mGravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
	}

	public static void setAccTextView(String text){
		accMeter.setText(text);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	protected void onResume() {
		super.onResume();
		resumeListners();
	}

	protected void onPause() {
		super.onPause();
		pauseListners();
	}

	private static void resumeListners(){
		mSensorManager.registerListener(accHandler, mAccSensor, SensorManager.SENSOR_DELAY_NORMAL);
		mSensorManager.registerListener(accHandler, mGravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
	}
	private static void pauseListners(){
		mSensorManager.unregisterListener(accHandler);
	}

	public  void relaxListning(){

		pauseListners();
		System.out.println("Paused listners");
		activityLayout.setBackgroundColor(Color.RED);

		final Timer  t = new Timer();     
		TimerTask task = new TimerTask() {  

			@Override  
			public void run() {  

				t.cancel();
				resumeListners();
				System.out.println("Resumed Listners");

				runOnUiThread(new Runnable() {  

					@Override  
					public void run() {  
						activityLayout.setBackgroundColor(Color.WHITE);
					}  
				});  
			}  
		};  
		t.scheduleAtFixedRate(task, 5000, 5000);  
	} 

	public  void playNotificationSound(){
		
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				try {
				      //  Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
				        
				        Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
				        if(alert == null){
				            // alert is null, using backup
				            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
				            if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
				                // alert backup is null, using 2nd backup
				                alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
				            }
				        }
				       // Context ctx = getApplicationContext();
				        Ringtone r = RingtoneManager.getRingtone(activityContext, alert);
				        r.play();
				    } catch (Exception e) {
				    	e.printStackTrace();
				    }
				
			}  });
		
		
	}
	
}
